<?php
/**
 *
 * Meta-Generator Validierung
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


class Yapary_Form_Formular_Meta_Validation
{
	private $oSimpleXmlElement		= NULL;


	/**
	 * */
	function __construct (SimpleXMLElement $_oSimpleXmlElement) {
		$this->oSimpleXmlElement = $_oSimpleXmlElement;
	}

	private function getObject ($_data) {
		$oObject = NULL;

		preg_match('/([a-z0-9_]*)\(([a-z0-9_\'\"\,\{\}\-\> ]*)\)(\-\>([a-z0-9_]*)\(([a-z0-9_\'\"\,\(\)\{\}\-\> ]*)\);?)?/ims', $_data, $arrResult);

		if (count($arrResult) >= 3) {
			$class		= $arrResult[1];
			$classParam = $arrResult[2];
			$classParam = str_replace("'", "", $classParam);
			$classParam = str_replace('"', '', $classParam);

			$arrParamClass = $this->getLocalReference(explode(',', $classParam));
			$oObject = $this->getObjectInstance($class, $arrParamClass);
		}
		if (count($arrResult) == 6) {
			$method		= $arrResult[4];
			$methodParam= $arrResult[5];
			$methodParam = str_replace("'", "", $methodParam);
			$methodParam = str_replace('"', '', $methodParam);

			$arrParamMethod = $this->getLocalReference(explode(',', $methodParam));
			return $this->getMethodInstance($oObject, $method, $arrParamMethod);
		}

		return $oObject;
	}


	private function getLocalReference ($_arrParam) {
		if (count($_arrParam)) {
			foreach ($_arrParam as $key => $val) {
				preg_match('/\{\{([a-z0-9\-\>]*)\}\}/ims', $val, $arrReference);
				if (count($arrReference)) {
					$reference = $arrReference[1];
					$arrPath = explode('->', $arrReference[1]);
					if (count($arrPath)) {
						$path = $this->oSimpleXmlElement;
						foreach ($arrPath as $key2 => $val2) {
							$path = $path->$val2;
						}
					}

					$path = (string) $path;
					$_arrParam[$key] = $this->getObject($path);
				}
			}
		}

		return $_arrParam;
	}

	private function getObjectInstance ($_objectName, array $_arrVar) {
		$arrVar[0] = NULL;
		$arrVar[1] = NULL;
		$arrVar[2] = NULL;
		$arrVar[3] = NULL;
		$arrVar[4] = NULL;
		$arrVar[5] = NULL;

		if (count($_arrVar)) {
			$i = 0;
			foreach ($_arrVar as $key => $val) {
				$arrVar[$i++] = $val;
			}
		}

		return new $_objectName($arrVar[0], $arrVar[1], $arrVar[2], $arrVar[3], $arrVar[4], $arrVar[5]);
	}

	private function getMethodInstance ($_object, $_methodName, array $_arrVar) {
		$arrVar[0] = NULL;
		$arrVar[1] = NULL;
		$arrVar[2] = NULL;
		$arrVar[3] = NULL;
		$arrVar[4] = NULL;
		$arrVar[5] = NULL;

		if (count($_arrVar)) {
			$i = 0;
			foreach ($_arrVar as $key => $val) {
				$arrVar[$i++] = $val;
			}
		}

		return $_object->$_methodName($arrVar[0], $arrVar[1], $arrVar[2], $arrVar[3], $arrVar[4], $arrVar[5]);
	}


	public function getValidation () {
		$result = '';
		$option = '';

		$objectName	= (string) $this->oSimpleXmlElement->validation->object;
		if (strlen($objectName) == 0) {
			return NULL;
		}

		$oObject = $this->getObject($objectName);
		return $oObject;
	}


}


?>